// pages/file/file.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filePath:'',
    fileList:[],
    key:'',
    value:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 保存文件
  saveFile:function(){
    var that = this
    wx.chooseImage({
      success: function(res) {
        var temFilePath = res.tempFilePaths
        wx.saveFile({
          tempFilePath: temFilePath[0],
          success:function(res){
            that.setData({
              filePath:res.savedFilePath
            })
            console.log(res.savedFilePath)
          }
        })
      },
    })
    wx.chooseLocation({
      success: function(res) {
        var temFilePath = res.tempFilePaths
        console.log(temFilePath)
      },
    })
  },
  // 获取缓存文件信息
  getFileInfo:function(){
    wx.getFileInfo({
      filePath: this.data.filePath,
      success(res){
        console.log(res.size)
        console.log(res.digest)
        console.log(res.errMsg)
      }
    })
  },
  // 获取已保存的文件列表
  getSaveFileList:function(){
    var that = this
    wx.getSavedFileList({
      success:function(res){
        console.log(res.fileList)
        that.setData({
          fileList:res.fileList
        })
      }
    })
  },
  //获取本地文件信息
  getSavedFileInfo:function(e){
    console.log(e)
    var index = e.target.id
    var file = this.data.fileList[index].filePath
    wx.getSavedFileInfo({
      filePath: file,
      success:function(res){
        console.log(res.size)
        console.log(res.createTime)
      }
    })
  },
  // 删除本地文件
  removeSaveFile:function(e){
    var index = e.target.id
    var file = this.data.fileList[index].filePath
    var that = this
    wx.removeSavedFile({
      filePath: file,
      success: function (res) {
        var arr = that.data.fileList
        arr.splice(index,1)
        that.setData({
          fileList: arr
        })
      }
    })
  },
  // 打开文件
  openFile:function(e){
    var index = e.target.id
    var file = this.data.fileList[index].filePath
    var that = this
    wx.openDocument({
      filePath: file,
      success:function(res){
        console.log('文件打开成功',res)
      },
      fail:function(res){
        console.log('文件打开失败',res)
      },
      complete:function(res){
        console.log('完毕',res)
      }
    })
  },
  // key输入框
  bindKeyInput:function(e){
    console.log(e)
    this.setData({
      key:e.detail.value
    })
  },
  // value输入框
  bindValueInput:function(e){
    this.setData({
      value: e.detail.value
    })
    
  },
  // 本地缓存
  setStorage:function(){
    wx.setStorage({
      key: this.data.key,
      data: this.data.value,
      success:function(){
        console.log('保存成功')
      }
    })
  },
  // 读取本地缓存
  getStorage:function(){
    wx.getStorage({
      key: this.data.key,
      success: function(res) {
        console.log(res.data)
      },
    })
  },
  // 获取缓存信息
  getStorgeInfo:function(){
    var that = this
    wx.getStorageInfo({
      success: function(res) {
        that.setData({
          key:res.keys[0]
        })
        console.log(res.keys)
        console.log(res.currentSize)
        console.log(res.limitSize)
      },
    })
  },
  // 移除缓存
  removeStroge:function(){
    wx.removeStorage({
      key: this.data.key,
      success: function(res) {
        console.log('移除',res.data)
      },
    })
  },
  // 清除缓存
  clearStroge:function(){
    try{
      wx.clearStorageSync()
    }catch(e){
      console.log('清除缓存失败',e)
    }
  }
})